package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 22:06
 **/
@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService service;

    @RequestMapping("")
    @PermissionName("角色主页")
    @RequiresPermissions("role:index")
    public String index() {
        return "role/list";
    }

    @RequestMapping("queryRoles")
    @ResponseBody
    public List<Role> queryRoles() {
        return service.list();
    }

    @RequestMapping("getRoleIdByEmpId")
    @ResponseBody
    public List<Long> getRoleIdByEmpId(Long empId) {
        return service.getRoleIdByEmpId(empId);
    }

    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("role:list")
    @PermissionName("角色数据")
    public PageResult list(QueryObject qo) {
        PageResult result = service.query(qo);
        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("role:save")
    @PermissionName("角色保存")
    public JsonResult save(Role entity) {
        JsonResult result = new JsonResult();
        try {
            service.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("role:update")
    @PermissionName("角色更新")
    public JsonResult update(Role entity) {
        JsonResult result = new JsonResult();
        try {
            service.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions("role:delete")
    @PermissionName("角色删除")
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            service.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("删除操作失败");
        }
        return result;
    }

}
